import { GetServerSideProps, GetServerSidePropsContext } from 'next'

export function withAuth(gssp?: GetServerSideProps) {
  return async (context: GetServerSidePropsContext) => {
    const token = context.req.cookies['token']
    
    if (!token) {
      return {
        redirect: {
          destination: '/login',
          permanent: false,
        },
      }
    }

    if (gssp) {
      return await gssp(context)
    }

    return {
      props: {}
    }
  }
}